﻿@{
    var X = Html.X();
}

@(
    X.Window()
        .ID("winEditarRoles")
        .Title(string.Format("Roles usuario \"{0}\"", ViewData["userName"]))
        .Resizable(false)
        .Icon(Icon.UserEdit)
        .Layout(LayoutType.Border)
        .Width(450)
        .Height(350)
        .Modal(true)
        .Constrain(true)
        //.BodyPadding(5)
        .Items(
            X.Hidden().ID("userName").Value(ViewData["userName"]),
            X.Menu()
                .ID("menuRoles")
                .Items(
                    X.Label()
                        .Cls("bold-text"),
                    X.MenuSeparator(),
                    X.MenuItem()
                        .Text("Eliminar")
                        .Icon(Icon.Delete)
                        .Handler("App.treePanelRoles.removeNode(App.treePanelRoles.menuNode);")
                )
                .Listeners(l =>
                {
                    l.Show.Handler = "this.getComponent(0).setText(this.nodeName);";
                }),
            X.Toolbar()
                .Region(Region.North)
                .Border(false)
                .Items(
                    X.Button()
                        .Text("Nuevo Rol")
                        .Icon(Icon.Add)
                        .DirectEvents(de =>
                        {
                            de.Click.Url = Url.Action("FrmRegistrar", "Roles");
                            de.Click.Before = "Ext.getBody().mask('Cargando...!', 'x-mask-loading');";
                            de.Click.Complete = "Ext.getBody().unmask();";
                        }),

                    X.ToolbarSeparator(),

                    X.Button()
                        .Icon(Icon.ArrowRefresh)
                        .Text("Recargar")
                        .Handler("App.storeRoles.reload({node: App.storeRoles.getNodeById('0')});")
                 ),
            X.TreePanel()
                .ID("treePanelRoles")
                .Region(Region.Center)
                .Border(false)
                .MultiSelect(false)
                .RootVisible(false)
                .Mode(TreePanelMode.Remote)
                .RemoteRemoveUrl(Url.Action("EliminarRol","Roles"))
                .Store(
                    X.TreeStore()
                        .ID("storeRoles")
                        .Parameters(X.StoreParameter().Name("userName").Mode(ParameterMode.Raw).Value("App.userName.getValue()"))
                        .Proxy(
                            X.AjaxProxy()
                                .Url(Url.Action("GetListRoles","Roles"))
                        )
                )
                .Root(X.Node().NodeID("0").Text("Roles"))
                .Listeners(l =>
                {
                    l.ItemContextMenu.Handler = @"var menu = App.menuRoles;
                                                this.menuNode = record;
                                                menu.nodeName = record.get('text');
                                                this.getSelectionModel().select(record);

                                                menu.showAt([e.getXY()[0], e.getXY()[1] + 10]);
                                                e.stopEvent();";
                    l.ItemContextMenu.StopEvent = true;
                    l.CheckChange.Handler = @"Usuarios.Admin.AsignarRol(App.userName.getValue(),(App.treePanelRoles.getSelectionModel()).getSelection()[0].data.id,checked,{
                                            success : function(resul) {
                                                if(!resul.success){
                                                    PcaGlobal.showError(resul.msg);
                                                    return;
                                                }
                                            }});";

                    l.RemoteActionRefusal.Handler = "PcaGlobal.showError(e.message);";
                    
                    l.AfterRender.Handler = "this.getRootNode().expand(true);";
                })
        )
)